package yawning.mew.equipment;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import yawning.mew.equipment.RawItem.Property;

public class EquipSet {
	public enum Slot {
		// Note: The slot names are written to match what ArmoryToonBuilder expects to be in the JSON.
		HEAD("head"),
		NECK("neck"),
		SHOULDER("shoulder"),
		BACK("back"),
		CHEST("chest"),
		WRIST("wrists"),
		HANDS("hands"),
		WAIST("waist"),
		LEGS("legs"),
		FEET("feet"),
		FINGER_1("finger1"),
		FINGER_2("finger2"),
		TRINKET_1("trinket1"),
		TRINKET_2("trinket2"),
		MAIN_HAND("mainHand"),
		OFF_HAND("offHand"),
		RANGED("ranged");

		private Slot(String name) {
			mName = name;
		}

		@Override
		public String toString() {
			return mName;
		}

		private String mName;
	}

	private final static List<Slot> sArmorSpecList = Arrays.asList(
			Slot.HEAD,
			Slot.SHOULDER,
			Slot.CHEST,
			Slot.WRIST,
			Slot.HANDS,
			Slot.WAIST,
			Slot.LEGS,
			Slot.FEET);

	public Map<Slot,Item> mEquipmentMap = new HashMap<Slot,Item>();

	public void addItem(Slot slot, Item item) {
		mEquipmentMap.put(slot, item);
	}

	public void addItem(Item item) {
		int slotbak = item.mItem.properties.get(Property.SLOT).intValue();
		switch (slotbak) {
			case 1: mEquipmentMap.put(Slot.HEAD, item); break;
			case 2: mEquipmentMap.put(Slot.NECK, item); break;
			case 3: mEquipmentMap.put(Slot.SHOULDER, item); break;
			case 4: break; // Shirt
			case 5: mEquipmentMap.put(Slot.CHEST, item); break;
			case 6: mEquipmentMap.put(Slot.WAIST, item); break;
			case 7: mEquipmentMap.put(Slot.LEGS, item); break;
			case 8: mEquipmentMap.put(Slot.FEET, item); break;
			case 9: mEquipmentMap.put(Slot.WRIST, item); break;
			case 10: mEquipmentMap.put(Slot.HANDS, item); break;
			case 11:
				if (!mEquipmentMap.containsKey(Slot.FINGER_1)) mEquipmentMap.put(Slot.FINGER_1, item);
				else mEquipmentMap.put(Slot.FINGER_2, item);
				break;
			case 12:
				if (!mEquipmentMap.containsKey(Slot.TRINKET_1)) mEquipmentMap.put(Slot.TRINKET_1, item);
				else mEquipmentMap.put(Slot.TRINKET_2, item);
				break;
			case 13: mEquipmentMap.put(Slot.MAIN_HAND, item); break;
			case 14: mEquipmentMap.put(Slot.OFF_HAND, item); break;	// Shields
			case 15: mEquipmentMap.put(Slot.RANGED, item); break;
			case 16: mEquipmentMap.put(Slot.BACK, item); break;
			case 17: mEquipmentMap.put(Slot.MAIN_HAND, item); break;
			case 21: mEquipmentMap.put(Slot.MAIN_HAND, item); break;
			case 19: break; // Tabard (Do some cloaks appear here?)
			case 20: mEquipmentMap.put(Slot.CHEST, item); break;
			case 25: mEquipmentMap.put(Slot.RANGED, item); break;
			case 26: mEquipmentMap.put(Slot.RANGED, item); break;
			case 28: mEquipmentMap.put(Slot.RANGED, item); break;
			default:
				System.out.println("** EquipSet: Unknown slot number " + slotbak + " for item: " + item.mItem);
				break;
		}
	}

	public boolean checkArmorSpec(int subClass) {
		boolean rval = true;
		for (Slot s : sArmorSpecList) {
			// How does armor spec work if you have a slot empty?  (Assume it's ok for now)
			if (mEquipmentMap.containsKey(s)) {
				Item item = mEquipmentMap.get(s);

				// If we are using the fast item source we won't have data for this.
				rval &= item.mItem.properties.containsKey(Property.SUBCLASS) ? (item.mItem.properties.get(Property.SUBCLASS).intValue() == subClass) : true;
			}
		}

		return rval;
	}

	public int sumProperty(Property p) {
		int total = 0;
		for (Item i : mEquipmentMap.values()) total += i.sumProperty(p);
		return total;
	}

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		for (Map.Entry<Slot, Item> e : mEquipmentMap.entrySet()) sb.append(e.getKey()).append(": ").append(e.getValue()).append('\n');
		return sb.toString();
	}
}

